package com.example.algorithm.ETree;

/**
 * @author: dangwen
 * @createDate: 2021/7/10
 * 剑指 Offer 68 - II. 二叉树的最近公共祖先
 */
public class LowestCommonAncestor {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

        if(root == null || p == root || q == root){
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left, p , q);
        TreeNode right = lowestCommonAncestor(root.left, p , q);

        return left == null ? right : (right == null ? left : root);
    }
}
